package com.chexiehui.jc.mvp.forum;

import com.chexiehui.jc.Constants;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePage;
import com.chexiehui.jc.base.BasePresenter;
import com.chexiehui.jc.entity.PostCommentInfo;
import com.chexiehui.jc.entity.PostInfo;

/**
 * @Description 帖子详情
 * @Auth SongJun
 * @Date 2019/11/20 10:05
 **/
public class ForumDynamicDetailsPresenter extends BasePresenter<ForumDynamicDetailsCovenant.View, ForumDynamicDetailsCovenant.Stores>
        implements ForumDynamicDetailsCovenant.Presenter {

    public ForumDynamicDetailsPresenter(ForumDynamicDetailsCovenant.View view) {
        attachView(view);
    }

    @Override
    public void getPostInfo() {
        addSubscription(appStores.getPostInfo(mvpView.getPostId()),
                new ApiCallback<BaseModel<PostInfo>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<PostInfo> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null) {
                                mvpView.onGetPostInfoSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "没有找到任何数据");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetPostInfoFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void getCommentList(int pageNum) {
        addSubscription(appStores.getCommentList(mvpView.getPostId(), pageNum, 10),
                new ApiCallback<BaseModel<BasePage<PostCommentInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<BasePage<PostCommentInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().getItems() != null &&
                                    model.getData().getItems().size() > 0) {
                                mvpView.onGetPostCommentListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "没有找到任何数据");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetPostCommentListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void addComment(String postId, String content) {
        mvpView.showLoading("正在提交");
        addSubscription(appStores.addComment(postId, content),
                new ApiCallback<BaseModel<PostCommentInfo>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<PostCommentInfo> model) {
                        if (model.getResultCode() == 0) {
                            if(model.getData() != null){
                                mvpView.hide();
                                mvpView.onAddCommentSuccess(model);
                            }else{
                                onFailure(-1, "评论失败");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onAddCommentFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void doDianZan(String objId, int position) {
        addSubscription(appStores.doDianZan(objId, position < 0 ? "1" : "2"),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            BaseModel<Integer> m = new BaseModel<>(model.getResultCode(), model.getResultInfo());
                            m.setData(position);
                            mvpView.onDoDianZanSuccess(m);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onDoDianZanFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void doShouCang(String objId) {
        addSubscription(appStores.doShouCang(objId, "0"),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.onDoShouCangSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onDoShouCangFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void doFollowPerson(String objId) {
        addSubscription(appStores.doFollowPerson(objId, "0"),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.onDoFollowPersonSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onDoFollowPersonFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void doReport(String id, String title) {
        mvpView.showLoading("正在提交");
        addSubscription(appStores.doReport(id, title, "1"),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.hide();
                            mvpView.onDoReportSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onDoReportFailure(new BaseModel<>(code, msg));
                    }
                });
    }
}